package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CatalogDao;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CatalogService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-24 15:43
 */
public class CatalogServiceImpl implements CatalogService {

    @Override
    public List<Catalog> findAll() {
        List<Catalog> catalogList;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            catalogList = catalogDao.findAll();
            return catalogList;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<Catalog> findAll(int page, int size) {
        List<Catalog> catalogList;
        SqlSession sqlSession = null;
        PageInfo pageInfo;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            PageHelper.startPage(page, size);
            catalogList = catalogDao.findAll();
            pageInfo = new PageInfo<>(catalogList);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Catalog findById(String id) {
        Catalog catalog;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            catalog = catalogDao.findById(id);
            return catalog;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int delete(Catalog catalog) {
        int del = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            del = catalogDao.delete(catalog.getId());
            //提交事务
            TransactionUtil.commit(sqlSession);
            return del;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int update(Catalog catalog) {
        int update = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            update = catalogDao.update(catalog);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return update;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int save(Catalog catalog) {
        int save = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession, CatalogDao.class);
            String uuid = UUID.randomUUID().toString();
            catalog.setId(uuid);
            save = catalogDao.save(catalog);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return save;
        } catch (Exception e) {

            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
